package com.my.xinyidaixi.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import com.my.xinyidaixi.DataBase.Bean.Address;
import com.my.xinyidaixi.R;

import java.util.ArrayList;

/**
 *  地址列表的适配器
 */
public class ListViewSlideAdapter extends BaseAdapter {
    private ArrayList<Address> bulbList;
    private Context context;
    private OnClickListenerEditOrDelete onClickListenerEditOrDelete;
    private boolean flag;

    public ListViewSlideAdapter(Context context, ArrayList<Address> bulbList,boolean flag){
        this.bulbList=bulbList;
        this.context=context;
        this.flag=flag;
    }

    @Override
    public int getCount() {

        return bulbList.size();
    }

    @Override
    public Object getItem(int position) {
        return bulbList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Address item=bulbList.get(position);
        View view;
        ViewHolder viewHolder;
        if(null == convertView) {
            view = View.inflate(context, R.layout.item_address, null);
            viewHolder=new ViewHolder();
            viewHolder.item_address_name=(TextView)view.findViewById(R.id.item_address_name);
            viewHolder.item_address_tel= (TextView) view.findViewById(R.id.item_address_tel);
            viewHolder.item_address_address=(TextView)view.findViewById(R.id.item_address_address);
            viewHolder.item_address_sex= (TextView) view.findViewById(R.id.item_address_sex);
            viewHolder.radiobutton= (RadioButton) view.findViewById(R.id.item_address_radiobutton);

            if(flag)
            {
                viewHolder.radiobutton.setVisibility(View.VISIBLE);
            }
            else
            {
                viewHolder.radiobutton.setVisibility(View.INVISIBLE);
            }

            viewHolder.tvEdit=(TextView)view.findViewById(R.id.tvEdit);
            viewHolder.tvDelete= (TextView) view.findViewById(R.id.delete);
            view.setTag(viewHolder);//store up viewHolder
        }else {
            view=convertView;
            viewHolder=(ViewHolder)view.getTag();
        }

        viewHolder.item_address_name.setText(item.getName());
        viewHolder.item_address_tel.setText(item.getDizhi_telphonenum());
        viewHolder.item_address_address.setText(item.getSchool().toString()+item.getAddress().toString());
        viewHolder.item_address_sex.setText(item.getSex());

        viewHolder.radiobutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListenerEditOrDelete!=null){
                    onClickListenerEditOrDelete.OnClickListenerRadioButton(position);
                }
            }
        });


        viewHolder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListenerEditOrDelete!=null){
                    onClickListenerEditOrDelete.OnClickListenerDelete(position);
                }
            }
        });
        viewHolder.tvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListenerEditOrDelete!=null){
                    onClickListenerEditOrDelete.OnClickListenerEdit(position);
                }
            }
        });
        return view;
    }

    private class ViewHolder
    {
        TextView item_address_name,tvEdit,tvDelete,item_address_tel,item_address_address,item_address_sex;
        RadioButton radiobutton;
    }

    //编辑和删除的接口
    public interface OnClickListenerEditOrDelete{
        void OnClickListenerEdit(int position);
        void OnClickListenerDelete(int position);
        void OnClickListenerRadioButton(int position);
    }

    //利用回调接口在activity中实现功能
    public void setOnClickListenerEditOrDelete(OnClickListenerEditOrDelete onClickListenerEditOrDelete1){
        this.onClickListenerEditOrDelete=onClickListenerEditOrDelete1;
    }
}
